package cn.tansanqinger.service.chatroom.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tansanqinger.dao.sql.chatroom.FriendlistSqlDaoImpl;
import cn.tansanqinger.dao.sql.chatroom.UnauthorizedSqlDaoImpl;
import cn.tansanqinger.dao.sql.user.UserMessageDispose;
import cn.tansanqinger.pojo.Friendlist;
import cn.tansanqinger.pojo.Unauthorized;
import cn.tansanqinger.pojo.User;
import cn.tansanqinger.service.chatroom.IUnauthorizedService;
import cn.tansanqinger.util.DateUtil;
import cn.tansanqinger.util.PageData;
import cn.tansanqinger.util.Tools;

@Service
public class UnauthorizedServiceImpl implements IUnauthorizedService{

	@Autowired
	private UnauthorizedSqlDaoImpl impl;
	@Autowired
	private UserMessageDispose udispose;
	@Autowired
	private FriendlistSqlDaoImpl fimpl;
	
	@Override
	public List<Unauthorized> selectAll(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		List<Unauthorized> unauthorizeds = impl.selectUnauthorizeds(user.getId());
		for (int i = 0; i < unauthorizeds.size(); i++) {
			if(unauthorizeds.get(i).getIspass()==0) {
				user = udispose.selectOneId(unauthorizeds.get(i).getAdduid());
				unauthorizeds.get(i).setPhone(user.getPhone());
				unauthorizeds.get(i).setUname(user.getName());
				if("".equals(Tools.isEmpty(user.getName()))) unauthorizeds.get(i).setUname(user.getPhone());
			} else {
				user = udispose.selectOneId(unauthorizeds.get(i).getBeuid());
				unauthorizeds.get(i).setPhone(user.getPhone());
				unauthorizeds.get(i).setUname(user.getName());
				if("".equals(Tools.isEmpty(user.getName()))) unauthorizeds.get(i).setUname(user.getPhone());
			}
		}
		return unauthorizeds;
	}

	@Override
	public Unauthorized selectOneId(HttpServletRequest request) {
		return impl.selectOnt(Integer.parseInt(Tools.isEmpty(request.getParameter("id"))));
	}

	@Override
	public PageData add(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		Unauthorized unauthorized = new Unauthorized();
		unauthorized.setAdduid(user.getId());
		unauthorized.setBeuid(Integer.parseInt(request.getParameter("beuid")));
		unauthorized.setIsdispose(0);
		unauthorized.setIsselect(0);
		unauthorized.setMessage(Tools.isEmpty(request.getParameter("message")));
		unauthorized.setUag(Integer.parseInt(Tools.isEmpty(request.getParameter("uag"))));
		PageData pd = new PageData();
		if(impl.add(unauthorized)==1) {
			pd.put("code", "0");
			pd.put("msg", "添加成功");
		} else {
			pd.put("code", "-1");
			pd.put("msg", "添加失败");
		}
		return pd;
	}

	@Override
	public PageData modification(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageData delete(HttpServletRequest request) {
		String id = Tools.isEmpty(request.getParameter("id"));
		PageData pd = new PageData();
		if(id.matches("\\d+")) {
			if(impl.deleteOne(Integer.parseInt(id))==1) {
				pd.put("code", "0");
				pd.put("msg", "删除成功");
			} else {
				pd.put("code", "-1");
				pd.put("msg", "删除失败");
			}
		} else {
			pd.put("code", "-1");
			pd.put("msg", "数据错误");
		}
		return pd;
	}

	@Override
	public int selectNumber(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		return impl.selectNumber(user.getId());
	}

	@Override
	public PageData refusedToAdd(HttpServletRequest request) {
		String id = Tools.isEmpty(request.getParameter("id"));
		PageData pd = new PageData();
		if(id.matches("\\d+")) {
			Unauthorized unauthorized = selectOneId(request);
			if(unauthorized!=null) {
				unauthorized.setMessage(Tools.isEmpty(request.getParameter("message")));
				unauthorized.setIsdispose(1);
				unauthorized.setIspass(1);
				int uid = unauthorized.getAdduid();
				unauthorized.setAdduid(unauthorized.getBeuid());//进行交换数据
				unauthorized.setBeuid(uid);
				if(impl.modification(unauthorized)==1) {
					pd.put("code", "0");
					pd.put("msg", "处理成功");
				} else {
					pd.put("code", "-1");
					pd.put("msg", "处理失败");
				}
			} else {
				pd.put("code", "-1");
				pd.put("msg", "数据错误");
			}
			
		} else {
			pd.put("code", "-1");
			pd.put("msg", "数据错误");
		}
		return pd;
	}

	@Override
	public PageData agreeToAdd(HttpServletRequest request) {
		String id = Tools.isEmpty(request.getParameter("id"));
		PageData pd = new PageData();
		if(id.matches("\\d+")) {
			Unauthorized unauthorized = selectOneId(request);
			if(unauthorized!=null) {
				unauthorized.setIsdispose(1);
				if(impl.modification(unauthorized)==1) {
					Friendlist friendlist = new Friendlist();
					friendlist.setFcomment(Tools.isEmpty(request.getParameter("message")));//被添加者给添加者的备注
					friendlist.setFdata(DateUtil.getTime());
					friendlist.setFtime(System.currentTimeMillis()/1000);
					friendlist.setUid(unauthorized.getBeuid());
					friendlist.setIid(unauthorized.getAdduid());
					fimpl.add(friendlist);
					friendlist.setFcomment("");
					friendlist.setUid(unauthorized.getAdduid());
					friendlist.setIid(unauthorized.getBeuid());
					fimpl.add(friendlist);
					pd.put("code", "0");
					pd.put("msg", "处理成功");
					//后续需要通知websocket进行消息广播，将数据双双交到对方手上
				} else {
					pd.put("code", "-1");
					pd.put("msg", "处理失败");
				}
			} else {
				pd.put("code", "-1");
				pd.put("msg", "数据错误");
			}
		} else {
			pd.put("code", "-1");
			pd.put("msg", "数据错误");
		}
		return pd;
	}

}
